import 'package:flutter/material.dart';
import 'dart:math';

/*
    双色球主页面
*/
class MyLotto extends StatefulWidget {
  const MyLotto({super.key});

  @override
  State<StatefulWidget> createState() => LottoPage();
}

class LottoPage extends State<MyLotto> {
  final blueInp = TextEditingController();
  final redInp = TextEditingController();
  final resInp = TextEditingController();

  List<int> arr = []; // 红码
  List<int> arr2 = []; //红码去重
  List<int> rd = []; // 蓝码
  List<int> resAry = []; // 结果

  // 初始化赋值
  @override
  void initState() {
    super.initState();
    updateVal();
  }

  // 生成随机数
  int randomInRange(int min, int max) {
    return min + Random().nextInt(max - min + 1);
  }

  // 随机
  void updateVal() {
    arr = [
      randomInRange(1, 33),
      randomInRange(1, 33),
      randomInRange(1, 33),
      randomInRange(1, 33),
      randomInRange(1, 33),
      randomInRange(1, 33),
    ];
    arr2 = [
      randomInRange(1, 6),
      randomInRange(7, 12),
      randomInRange(13, 18),
      randomInRange(19, 24),
      randomInRange(25, 30),
      randomInRange(31, 33),
    ];
    rd = [(1 + Random().nextInt(16))];

    arr.sort((a, b) => a - b);
    arr2.sort((a, b) => a - b);

    for (int i = 0; i < arr.length - 1; i++) {
      //红码去重处理
      if (arr[i] == arr[i + 1]) {
        arr = arr2;
      }
    }

    blueInp.text = arr.join(',');
    redInp.text = rd.join('');
    resInp.text = '${arr.join(' ')}  ${redInp.text}';

    // 动态刷新双色球
    setState(() {
      resAry = arr + rd;
    });
  }

  @override
  Widget build(BuildContext context) {
    String imgUrl = 'assets/img/caipiao.jpg';
    return Container(
      width: double.infinity,
      height: double.infinity,
      padding: EdgeInsets.fromLTRB(20, 20, 20, 50),
      decoration: BoxDecoration(
        color: Colors.white,
        image: DecorationImage(image: AssetImage(imgUrl), fit: BoxFit.cover),
      ),
      child: Column(
        children: [
          MyInp(
            btnText: '红球',
            fontColor: Colors.redAccent,
            btnColor: Colors.redAccent,
            inpContro: blueInp,
          ),
          MyInp(
            btnText: '蓝球',
            fontColor: Colors.blueAccent,
            btnColor: Colors.blueAccent,
            inpContro: redInp,
          ),
          ResultBox(
            btnText: '结果',
            btnColor: Colors.deepPurpleAccent,
            ballText: resAry,
          ),
          Spacer(),
          SizedBox(
            width: 300,
            height: 60,
            child: ElevatedButton(
              onPressed: updateVal,
              style: ButtonStyle(
                backgroundColor: WidgetStatePropertyAll(Colors.blue),
              ),
              child: Text(
                "随机一注",
                style: TextStyle(color: Colors.white, fontSize: 28),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

/*
    输入框
*/

class MyInp extends StatefulWidget {
  final Color btnColor;
  final Color fontColor;
  final String btnText;
  final TextEditingController inpContro;
  const MyInp({
    super.key,
    required this.btnColor,
    required this.fontColor,
    required this.btnText,
    required this.inpContro,
  });

  @override
  State<StatefulWidget> createState() {
    return InputPart();
  }
}

class InputPart extends State<MyInp> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Flex(
        direction: Axis.horizontal,
        children: [
          Expanded(
            child: Container(
              height: 70,
              margin: EdgeInsets.fromLTRB(0, 15, 0, 15),
              transform: Matrix4.translationValues(5, 0, 0),
              decoration: BoxDecoration(
                color: widget.btnColor,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10),
                  bottomLeft: Radius.circular(10),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Icon(Icons.sports_soccer, color: Colors.white, size: 28),
                  Text(
                    widget.btnText,
                    style: TextStyle(color: Colors.white, fontSize: 28),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 6,
            child: Center(
              child: SizedBox(
                height: 70,
                child: TextField(
                  controller: widget.inpContro,
                  enabled: false,
                  style: TextStyle(
                    color: widget.fontColor,
                    fontSize: 30,
                    fontWeight: FontWeight.w600,
                  ),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    filled: true,
                    fillColor: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

/*
    结果展示
*/

class ResultBox extends StatefulWidget {
  final Color btnColor;
  final String btnText;
  final List<int> ballText;
  const ResultBox({
    super.key,
    required this.btnColor,
    required this.btnText,
    required this.ballText,
  });

  @override
  State<StatefulWidget> createState() {
    return ResItem();
  }
}

class ResItem extends State<ResultBox> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Flex(
        direction: Axis.horizontal,
        children: [
          Expanded(
            child: Container(
              height: 70,
              margin: EdgeInsets.fromLTRB(0, 15, 0, 15),
              transform: Matrix4.translationValues(5, 0, 0),
              decoration: BoxDecoration(
                color: widget.btnColor,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10),
                  bottomLeft: Radius.circular(10),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Icon(Icons.sports_soccer, color: Colors.white, size: 28),
                  Text(
                    widget.btnText,
                    style: TextStyle(color: Colors.white, fontSize: 28),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 6,
            child: Container(
              height: 70,
              padding: EdgeInsets.only(left: 15),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(5)),
              ),
              child: Row(
                children: [
                  ResText(
                    inpVal: widget.ballText[0].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  ResText(
                    inpVal: widget.ballText[1].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  ResText(
                    inpVal: widget.ballText[2].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  ResText(
                    inpVal: widget.ballText[3].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  ResText(
                    inpVal: widget.ballText[4].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  ResText(
                    inpVal: widget.ballText[5].toString(),
                    bgColor: Colors.blueAccent,
                  ),
                  Spacer(),
                  ResText(
                    inpVal: widget.ballText[6].toString(),
                    bgColor: Colors.redAccent,
                  ),
                  Spacer(),
                  Spacer(),
                  Spacer(),
                  Spacer(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

// 结果数字样式
class ResText extends StatelessWidget {
  final String inpVal;
  final Color bgColor;
  const ResText({super.key, required this.inpVal, required this.bgColor});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 50,
      height: 50,
      margin: EdgeInsets.only(right: 10),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.all(Radius.circular(25)),
      ),
      child: Center(
        child: Text(
          inpVal,
          style: TextStyle(
            color: Colors.white,
            fontSize: 26,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
    );
  }
}
